﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;

    public class PrintSettingApp : IPrintSetting
    {
        private string dbname = "Systems";

        public void Add(PrintSettingEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_PrintSetting(");
            builder.Append("ChainNum,DocumentName,PageSize,LeftMargin,RightMargin,TopMargin,BottomMargin,HengXiangZongXiang");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.DocumentName + "',");
            builder.Append("'" + model.PageSize + "',");
            builder.Append("" + model.LeftMargin + ",");
            builder.Append("" + model.RightMargin + ",");
            builder.Append("" + model.TopMargin + ",");
            builder.Append("" + model.BottomMargin + ",");
            builder.Append("" + model.HengXiangZongXiang + "");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int SettingId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete T_PrintSetting ");
            builder.Append(" where SettingId=" + SettingId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PrintSetting ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by SettingId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "SettingId", "T_PrintSetting", swhere);
        }

        public PrintSettingEnt GetModel(int SettingId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SettingId,ChainNum,DocumentName,PageSize,LeftMargin,RightMargin,TopMargin,BottomMargin,HengXiangZongXiang from T_PrintSetting ");
            builder.Append(" where SettingId=" + SettingId);
            PrintSettingEnt ent = new PrintSettingEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.SettingId = SettingId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.DocumentName = table.Rows[0]["DocumentName"].ToString();
                ent.PageSize = table.Rows[0]["PageSize"].ToString();
                if (table.Rows[0]["LeftMargin"].ToString() != "")
                {
                    ent.LeftMargin = decimal.Parse(table.Rows[0]["LeftMargin"].ToString());
                }
                if (table.Rows[0]["RightMargin"].ToString() != "")
                {
                    ent.RightMargin = decimal.Parse(table.Rows[0]["RightMargin"].ToString());
                }
                if (table.Rows[0]["TopMargin"].ToString() != "")
                {
                    ent.TopMargin = decimal.Parse(table.Rows[0]["TopMargin"].ToString());
                }
                if (table.Rows[0]["BottomMargin"].ToString() != "")
                {
                    ent.BottomMargin = decimal.Parse(table.Rows[0]["BottomMargin"].ToString());
                }
                if (table.Rows[0]["HengXiangZongXiang"].ToString() != "")
                {
                    ent.HengXiangZongXiang = int.Parse(table.Rows[0]["HengXiangZongXiang"].ToString());
                }
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_PrintSetting ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "SettingId");
        }

        public void Update(PrintSettingEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_PrintSetting set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("DocumentName='" + model.DocumentName + "',");
            builder.Append("PageSize='" + model.PageSize + "',");
            builder.Append("LeftMargin=" + model.LeftMargin + ",");
            builder.Append("RightMargin=" + model.RightMargin + ",");
            builder.Append("TopMargin=" + model.TopMargin + ",");
            builder.Append("BottomMargin=" + model.BottomMargin + ",");
            builder.Append("HengXiangZongXiang=" + model.HengXiangZongXiang + "");
            builder.Append(" where SettingId=" + model.SettingId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

